<?php
/**
 * @author Haohuang
 * @email  huanghao1054@gmail.com
 * @since   2019/8/10
 */

namespace App\Console\Commands\TxMan;


use App\Service\LogTrait;
use App\Service\Man\TxMan\App;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class TxManImage extends Command
{
    use LogTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'txManImage';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws \Exception
     * @return mixed
     */
    public function handle()
    {
        try{
            $this->measureTime();
            $this->logName = $this->signature;

            $app = new App();
            \App\Models\ManDetail::query()
                ->select('man_id', 'chapter_url')
                ->where('platform_id', 2)
                ->chunk(1, function (Collection $result) use ($app) {
                    $result = $result->toArray();
                    $urls   = collect($result)->pluck('chapter_url')->map(function ($val) {
                        return json_decode($val, true);
                    })->collapse()->toArray();
                    $manId = current($result)['man_id'];
                    $imageList = $app->image($urls);
                    $this->saveListImage($imageList, $manId);
                });


        }catch (\Exception $e){
            $this->logWrite($e->getMessage());
            $this->logWrite($e->getTraceAsString());
        }
    }

}